Structures in Golo

Getting Started with Golo

GoloScript uses structs to define custom types and augmentation to add methods to them.


Defining and Using Structs

struct Person = { name, age }

let alice = Person("Alice", 30)

# Read fields
println(alice: name())  # Alice
println(alice: age())   # 30

# Modify fields
alice: age(31)
println(alice)  # Person(name=Alice, age=31)

Chained Initialization

Setters return the struct, so you can chain calls.

struct Configuration = { host, port, debug }

let config = Configuration("", 0, false)
  : host("localhost")
  : port(8080)
  : debug(true)

Copy and Freeze

let alice = Person("Alice", 30)

# Mutable copy
let bob = alice: copy(): name("Bob"): age(28)

# Immutable copy
let frozen = alice: frozenCopy()
println(frozen: isFrozen())  # true
# frozen: age(31)  # Error! Cannot modify

Introspection

let alice = Person("Alice", 30)

println(alice: members())    # [name, age]
println(alice: values())     # [Alice, 30]
println(alice: get("name"))  # Alice
alice: set("age", 31)

Simple Augmentation

Augmentation adds methods to a struct. The first parameter this refers to the instance.

struct Person = { name, age }

augment Person {
  function greet = |this| {
    return "Hello, I'm " + this: name()
  }

  function birthday = |this| {
    this: age(this: age() + 1)
  }
}

let alice = Person("Alice", 30)
println(alice: greet())  # Hello, I'm Alice
alice: birthday()
println(alice: age())    # 31

Named Augmentations (Reusable Traits)

Named augmentations can be shared across multiple structs.

augmentation Runnable = {
  function run = |this| {
    println(this: name() + " is running!")
  }
}

augmentation Woofable = {
  function woof = |this| {
    println(this: name() + " says: Woof!")
  }
}

struct Dog = { name, age, breed }

# Apply multiple augmentations
augment Dog with Runnable, Woofable

let rex = Dog("Rex", 5, "German Shepherd")
rex: run()   # Rex is running!
rex: woof()  # Rex says: Woof!

© 2026 GoloScript Project | Built with Gu10berg

Subscribe: 📡 RSS | ⚛️ Atom